ActiveAdmin.register Stores::Report do

  actions :index
  filter :store_name, label: '门店'
  filter :coach_name, label: '门店经理'
  filter :coach_employee_id, label: '经理工号'
  filter :report_date

  action_item :report, only: :index do
    render partial: 'report'
  end

  csv do
    column :report_date do |item|
      item.report_date.strftime("%Y年%m月")
    end
    column :company_name
    column :store_name
    column '城市' do |item|
      item.store.city
    end
    column :coach_id
    column :coach_name
    column '经理工号' do |item|
      item.coach.try(:employee_id)
    end
    column :free
    column :tuition
    column '总课时' do |item|
      item.free + item.tuition
    end
    column :per_subsidy
    column :subsidy
  end

  index do
    column :report_date do |rd|
      rd.report_date.strftime("%Y年%m月")
    end
    column :company_name
    column :store_name
    column :coach_id
    column :coach_name
    column '经理工号' do |item|
      item.coach.try(:employee_id)
    end
    column :free
    column :tuition
    column '总课时' do |item|
      item.free + item.tuition
    end
  end

  controller do
    def scoped_collection
      Stores::Report.includes(:store, :coach)
    end

    def calculate
      StoreJob.set(wait: 1.seconds).perform_later(params[:stores_report][:store_id], params[:stores_report][:report_date])
      redirect_to collection_path, notice: '您选择门店的月报表已开始计算,请稍后查看'
    end
  end
end
